package com.common.widget.audio;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.starappallcommon.R;

/**
 * 自定义的“加载中”控件
 * 
 * <br>Created 2014-6-6 下午5:24:39
 * @version  
 * @author   cb		
 *
 * @see
 */
public class StarappVolumeView extends RelativeLayout {
	private final double mMaxVolume = 10000;
	private final int THREE = 3;
	private double mMinVolume = 0.0D;
	private int mVolume =0;
	private int mVolumeCount;
	private ArrayList<View> mViews=new ArrayList<View>();
	
	/**
	 * 
	 * Creates a new instance of StarappVolumeView.
	 * <br>Created 2014-6-9 下午5:37:13
	 * @param context 上下文
	 */
	public StarappVolumeView(Context context) {
		super(context);
		init(context);
	}
	
	/**
	 * 
	 * Creates a new instance of StarappVolumeView.
	 * <br>Created 2014-6-9 下午5:37:19
	 * @param context 上下文
	 * @param attrs 属性集
	 */
	public StarappVolumeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	/**
	 * 
	 * Creates a new instance of StarappVolumeView.
	 * <br>Created 2014-6-9 下午5:37:24
	 * @param context 上下文
	 * @param attrs 属性集
	 * @param defStyle style
	 */
	public StarappVolumeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	/**
	 * 初始化View
	 * 
	 * <br>Created 2014-6-9 下午5:38:36
	 * @param context 上下文
	 * @author        :  cb
	 */
	private void init(Context context) {
		RelativeLayout parent=(RelativeLayout) LayoutInflater.from(context).inflate(R.layout.starapp_volume_view, this);
		if (parent.getChildCount()>0 && parent.getChildAt(0) instanceof RelativeLayout){
			parent=(RelativeLayout) parent.getChildAt(0);
			mVolumeCount=parent.getChildCount();
			for (int i=0;i<mVolumeCount;i++){
				mViews.add(parent.getChildAt(i));
			}			
		}
	}
	
	/**
	 * 设置音量
	 * @param volume 不限大小
	 */
	public void setVolume(double volume) {
		if (volume < mMinVolume){
			// 低于最小值设该值为最小值
			mVolume=0;
		}else if (volume > mMaxVolume){
			// 高于最大值设该值为最大值
			mVolume=THREE;
		}else{
			mVolume =(int)((volume - mMinVolume) / (mMaxVolume - mMinVolume)*THREE);
		}
		for (int i=0;i<mVolumeCount;i++){
			if (i<=mVolume){
				mViews.get(i).setVisibility(View.VISIBLE);
			}else{
				mViews.get(i).setVisibility(View.INVISIBLE);
			}
		}
	}
}
